import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  ManyToOne,
  ManyToMany,
  JoinTable,
  OneToMany,
} from 'typeorm';
import { User } from '../../user/entities/user.entity';
import { Service } from '../../service/entities/service.entity';
import { Order } from '../../order/entities/order.entity';
import { v4 as uuidv4 } from 'uuid';
import { CarPhoto } from '../../car_photo/entities/car_photo.entity';
import { Cart } from '../../cart/entities/cart.entity';

@Entity('car')
export class Car {
  @PrimaryGeneratedColumn({ type: 'int', comment: 'id' })
  id: number;

  @Column({
    type: 'varchar',
    comment: '车辆编号',
    nullable: false,
    default: () => `'${uuidv4().replace(/-/g, '')}'`,
  })
  primary_id: string;

  @Column({
    type: 'boolean',
    comment: '是否被删除',
    default: false,
  })
  isDelete: boolean;

  @Column({ type: 'varchar', comment: '车牌号', unique: true, nullable: false })
  license_plate: string;

  @Column({ type: 'varchar', comment: '品牌', nullable: false })
  brand: string;

  @OneToMany(() => CarPhoto, (photo) => photo.car)
  photos: CarPhoto[];

  @Column({
    type: 'varchar',
    comment: '类型',
    nullable: true,
    default: 'car',
  })
  type: string;

  @Column({ type: 'varchar', comment: '车型', nullable: false })
  shape: string;

  @Column({ type: 'varchar', comment: '颜色' })
  color: string;

  @Column({ type: 'varchar', comment: '排量', nullable: false })
  displacement: string;

  @Column({
    type: 'varchar',
    comment: '车辆状态，0 运行中，1已修好， 2已修好, 3 ',
    nullable: false,
    default: 0,
  })
  status: string;

  @ManyToOne(() => User, (user) => user.car)
  user: User;

  @OneToMany(() => Order, (order) => order.car)
  order: Order[];

  @OneToMany(() => Cart, (cart) => cart.car)
  cart: Cart[];

  // 这个车的服务
  @ManyToMany(() => Service, (service) => service.car)
  @JoinTable()
  service: Service[];

  @Column({
    name: 'create_date',
    type: 'timestamp',
    comment: '创建时间',
    default: () => 'CURRENT_TIMESTAMP',
  })
  create_date: Date;

  @Column({
    name: 'update_date',
    type: 'timestamp',
    comment: '修改时间',
    nullable: true,
  })
  updated_date: Date;
}
